<?php

namespace App\Models;
use Illuminate\Database\Eloquent\Builder;

class Video extends BaseModel
{
    public static function filterData(Builder &$query, $filter=[]){
        if(isset($filter['title'])){
            $query->where('title', "like", "%{$filter['title']}%");
        }
        if(isset($filter['is_recommend']) && $filter['is_recommend']!==''){
            $query->where('is_recommend', '=', $filter['is_recommend']);
        }
        if(!empty($filter['time_begin'])){
            $query->where("created_at", ">=", $filter['time_begin']);
        }
        if(!empty($filter['time_end'])){
            $query->where("created_at", "<=", $filter['time_end']);
        }
    }

    public function images(){
        return $this->morphMany(Image::class,"imaged","used_for","used_id");
    }

    public function user(){
        return $this->belongsTo(User::class);
    }

//    public function comments(){
//        return $this->morphMany(Comment::class,"commentable","used_for", "used_id");
//    }

    public static function getQuery():Builder{
        $query = self::with(['images', 'user']);
//        if(!empty($keyword)){
//            $query->where("body","like","%".$keyword."%")
//                ->orWhereHas("user",function ($query)use ($keyword){
//                $query->where("nickname","like","%".$keyword."%")->orWhere("mobile","like","%".$keyword."%");
//            });
//        }
        return $query;
//        return $query->orderByDesc('id')->paginate($limit, ["*"], 'page', $page);
    }


}
